package com.cndw.kungfu.event.once;

import java.util.ArrayList;
import java.util.Collection;

import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.vo.PlayerChangeVO;

/**
 * 删除玩家某个状态的任务
 * @author fantadust [9677889@qq.com]
 * @date 2011-9-19 上午11:10:47
 */
public class PlayerStateTask implements Runnable {
	
	private Collection<Player> members;
	
	private int status;
	
	/**
	 * 
	 * @param status 需要删除的状态
	 * @param players
	 */
	public PlayerStateTask(int status,int seconds,Collection<Player> memberPlayers){
		this.members = memberPlayers;
		this.status = status;
		init(seconds);
	}
	
	public PlayerStateTask(int status,int seconds,Player player){
		this.members = new ArrayList<Player>();
		members.add(player);
		this.status = status;
		init(seconds);
	}	
	
	@Override
	public void run() {
		try {
			if (members != null && !members.isEmpty()) {
				for (Player e : members) {
					e.stateRemove(status);
					if (status == Constants.playerState32_vipAdd) {
						Context.getPlayerServer().clacProperties(e);
						Context.getPlayerServer().change(PlayerChangeVO.PLAYER_MAIN, e.toPlayerVO(), e.getPlayerId());
					}else {
						Context.getPlayerServer().broadcaseState(e);
					}
					// 广播状态
					
				}			
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		
	}
	
	private void init(int seconds){
		Context.getTimerServer().scheduleSeconds(this,seconds);	
	}

}
